package com.fjwt.gz.mgr.ctrl.award.goods;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.AwardGoodsDTO;
import com.fjwt.gz.db.vo.AwardGoodsVO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * 实物奖品管理
 * @Author: Will
 * @Date: 2023-12-26
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/api/award/goods")
public class AwardGoodsController extends CommonCtrl {
    @Autowired
    private AwardGoodsService awardGoodsService;

    /**
     * 获取未分配活动的实物奖品列表
     * @return
     */
    @GetMapping(value = "/usable")
    public ApiRes listWithoutActivity() {
        AwardGoodsDTO awardGoodsDTO = getObject(AwardGoodsDTO.class);
        log.info("pc端查询未分配的实物奖品列表，请求地址：/api/award/goods/usable，请求参数：{}",  JSONUtil.toJsonStr(awardGoodsDTO));
        List<AwardGoodsVO> list = awardGoodsService.queryList(awardGoodsDTO);
        log.info("pc端查询未分配的实物奖品列表，请求地址：/api/award/goods/usable，返回结果：{}",  JSONUtil.toJsonStr(list));
        return ApiRes.ok(list);
    }

    /**
     * 查询实物奖品列表
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_GOODS_INFO')")
    @GetMapping
    public ApiRes list() {
        AwardGoodsDTO awardGoodsDTO = getObject(AwardGoodsDTO.class);
        log.info("pc端查询实物奖品列表，请求地址：/api/award/goods，请求参数：{}",  JSONUtil.toJsonStr(awardGoodsDTO));
        IPage<AwardGoodsVO> page = awardGoodsService.queryList(getIPage(), awardGoodsDTO);
        log.info("pc端查询实物奖品列表，请求地址：/api/award/goods，返回结果：{}",  JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 查询实物奖品详情
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_GOODS_INFO_VIEW')")
    @GetMapping(value = "/details")
    public ApiRes details() {
        AwardGoodsDTO awardGoodsDTO = getObject(AwardGoodsDTO.class);
        log.info("pc端查询实物奖品详情，请求地址：/api/award/goods/details，请求参数：{}",  JSONUtil.toJsonStr(awardGoodsDTO));
        AwardGoodsVO awardGoodsVO = awardGoodsService.details(awardGoodsDTO);
        log.info("pc端查询实物奖品详情，请求地址：/api/award/goods/details，返回结果：{}",  JSONUtil.toJsonStr(awardGoodsVO));
        AwardGoodsVO data = new AwardGoodsVO();
        BeanUtils.copyProperties(awardGoodsVO, data);
        return ApiRes.ok(data);
    }

    /**
     * 新增实物奖品
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_GOODS_INFO_ADD', 'ENT_ACTIVITY_LIST_ADD')")
    @PostMapping(value = "")
    public ApiRes add() {
        AwardGoodsDTO awardGoodsDTO = getObject(AwardGoodsDTO.class);
        // 当前登录用户信息
        awardGoodsDTO.setCreatedUid(getCurrentUser().getSysUserId());
        log.info("pc端新增实物奖品，请求地址：/api/award/goods，请求参数：{}",  JSONUtil.toJsonStr(awardGoodsDTO));
        AwardGoodsVO awardGoodsVO = awardGoodsService.add(awardGoodsDTO);
        log.info("pc端新增实物奖品，请求地址：/api/award/goods，返回结果：{}",  JSONUtil.toJsonStr(awardGoodsVO));
        return ApiRes.ok(awardGoodsVO);
    }

    /**
     * 修改实物奖品
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_GOODS_INFO_EDIT')")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ApiRes update() {
        AwardGoodsDTO awardGoodsDTO = getObject(AwardGoodsDTO.class);
        log.info("pc端修改实物奖品，请求地址：/api/award/goods，请求参数：{}",  JSONUtil.toJsonStr(awardGoodsDTO));
        awardGoodsService.update(awardGoodsDTO);
        return ApiRes.ok();
    }

    /**
     * 更改实物奖品状态
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_GOODS_INFO_EDIT', 'ENT_ACTIVITY_LIST_ADD')")
    @RequestMapping(value = "/state", method = RequestMethod.PUT)
    public ApiRes updateState() {
        AwardGoodsDTO awardGoodsDTO = getObject(AwardGoodsDTO.class);
        log.info("pc端更改实物奖品状态，请求地址：/api/award/goods/state，请求参数：{}",  JSONUtil.toJsonStr(awardGoodsDTO));
        awardGoodsService.updateState(awardGoodsDTO);
        return ApiRes.ok();
    }

    /**
     * 删除实物奖品
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_GOODS_INFO_DEL')")
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ApiRes delete() {
        AwardGoodsDTO awardGoodsDTO = getObject(AwardGoodsDTO.class);
        log.info("pc端删除实物奖品，请求地址：/api/award/goods，请求参数：{}",  JSONUtil.toJsonStr(awardGoodsDTO));
        awardGoodsService.delete(awardGoodsDTO);
        return ApiRes.ok();
    }

}
